<?php

//User用户操作的model

namespace Home\Model;
use Think\Model;

class UserModel extends Model {
    /* 通过用户名和密码验证用户信息，登陆成功返回用户信息，失败则返回NULL */


    public function isUser($data) {
        $LoginName = $data['LoginName'];
        $UserPassword = $data['userPassword'];
        $sql = "SELECT * FROM " . $this->tablePrefix . "user where ( phoneNumber='" . $LoginName . "' OR userEmail='" . $LoginName . "') AND userPassword='" . $UserPassword . "'";
        $result = $this->query($sql); //执行sql语句，若存在用户则返回结果，如果不存在则返回NULL
        $result = $result[0]; //获取结果中的第一个一位数组，其实也只有一个
        if ($result) {
            $date = date('Y-m-d h:i:s'); //获取当前系统时间
            $user = M('user');
            //$user->latestLoginTime=$date;
            $sql = "UPDATE " . $this->tablePrefix . "user SET latestLoginTime='" . $date . "' WHERE ID=" . $result['id'];
            $res = $user->execute($sql); //执行sql语句，返回查询结果
            if (!$res) {
                return 0;
            }
        }
        return $result;
    }

    public function loginOut() {
        
    }

    /* 用户注册 */

    public function addUser($data) {
        $date = date('Y-m-d h:i:s');
        $data = $data + array('userCreateTime' => $date);
        $sql = "INSERT INTO " . $this->tablePrefix . ""
                . "user(school_ID,phoneNumber,userPassword,userCreateTime,userLotteryNumberInfo_id) "
                . "VALUES(" . $data['shool_ID'] . ",'" . $data['phoneNumber'] . "','" . $data['userPassword'] . "','" . $date . "'," . $data['userLotteryNumberInfo_id'] . ")";
        $result = $this->execute($sql);
        return $result;
    }

    /* 检查用户手机号是否是否已经被绑定 */

    public function checkUserPhone($phoneNumber) {      //可由方法  queryByPhone代替     
        $result = $this->where("phoneNumber='" . $phoneNumber . "'")->select();
        return $result;
    }

    /* 检查邮箱是否已经被绑定 */

    public function checkUserMail($Email) {
        $result = $this->where("userEmail='" . $Email . "'")->select();
        return $result;
    }

    /* 修改密码 */

    public function modifyPassword($userId, $password) {//修改用户密码
        $sql = "UPDATE " . $this->tablePrefix . "user SET userPassword='" . $password . "' WHERE ID=" . $userId;
        $result = $this->execute($sql);
        return $result;
    }

    /* 修改用户个人信息 */

    public function modifyUserInfo($data) {//修改用户信息
        $userID = $data['ID'];
        $result = $this->where("ID=" . $userID)->save($data);
        return $result;
    }

    public function queryByPhone($phoneNumber) {//通过手机号查询用户信息
        $result = $this->where("phoneNumber='" . $phoneNumber . "'")->select();
        return $result;
    }

    public function queryAll() {//查询所有用户信息
        $result = $this->select();
        return $result;
    }

}
